package de.fhaugsburg.games.boardgames.scrabble.gui;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import de.fhaugsburg.games.boardgames.scrabble.scrabbleboard.LetterPiece;

public class GLetterPiece extends JLayeredPane implements MouseListener, MouseMotionListener {

	
	
	public static enum GLetterState {
		BAR, DROPPING, DRAGGING, FIXED;
	}
	
	
	
	
	//FIXME: Bad!
	private static final String IMG = ScrabbleMainWindow.IMG;
	
	private BufferedImage backgroundImage, pieceImage;
	private LetterPiece letterPiece;
	private boolean draggable;
	private int xAdjust, yAdjust; 
	private GLetterState letterState, letterOrigin;
	
	
	
	
	
	public GLetterPiece(LetterPiece piece, GLetterState state) {
		this.letterPiece = piece;
		this.draggable = true;
		setLetterState(state);
		loadImages();
		setPreferredSize(new Dimension(30,30));
		setBorder(null);
		setSize(30, 30);
		setLocation(0, 0);
		this.pieceImage = renderLetter();
//		disableEvents(MouseEvent.MOUSE_PRESSED);
//		disableEvents(MouseEvent.MOUSE_DRAGGED);
//		this.addMouseListener(this);
//		this.addMouseListener((ScrabbleMainWindow) getParent());
//	    this.addMouseMotionListener(this);
//	    this.addMouseMotionListener((ScrabbleMainWindow) getParent());
	    setCursor( Cursor.getPredefinedCursor( Cursor.HAND_CURSOR ) );
	    
		repaint();
	}
	
	
	
	public LetterPiece getLetterPiece(){
		return this.letterPiece;
	}
	
	
	private void loadImages(){
		try {
			backgroundImage = ImageIO.read(new File(IMG + "letter_piece.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private BufferedImage renderLetter(){
		return GLetterPiece.drawLetter(this.letterPiece);
	}
	
	
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);

		//Graphics2D g2d = (Graphics2D) g;
		//g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		g.drawImage(pieceImage, 0, 0, pieceImage.getWidth(), pieceImage.getHeight(), this);
		
		/*
		//FIXME: use cross-plattform fonts
		Font font = new Font("Arial Black",Font.BOLD, 18);
		g2d.setFont(font);
		g2d.drawString(letterPiece.getSymbol(), 0, 20);
		
		//draw the letter value
		font = new Font("Arial",Font.PLAIN, 11);
		g2d.setFont(font);
		g2d.drawString(String.valueOf(letterPiece.getPoints()), 22, 25);
		*/
	}

	
	
	
	public static BufferedImage drawLetter(LetterPiece letterPiece){
//		Graphics2D stone = new Graphics2D();
		BufferedImage backgroundImage = null;
		try {
			backgroundImage = ImageIO.read(new File(IMG + "letter_piece.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
		Graphics2D g2d = (Graphics2D) backgroundImage.getGraphics();
		
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,         RenderingHints.VALUE_ANTIALIAS_ON);

		g2d.drawImage(backgroundImage, 0, 0, backgroundImage.getWidth(), backgroundImage.getHeight(), new JPanel());
		
		//FIXME: use cross-plattform fonts
		Font font = new Font("Arial Black",Font.BOLD, 18);
		g2d.setFont(font);
		g2d.setColor(Color.BLACK);
		g2d.drawString(letterPiece.getSymbol(), 0, 20);
		
		//draw the letter value
		font = new Font("Arial",Font.PLAIN, 11);
		g2d.setFont(font);
		g2d.setColor(Color.BLACK);
		g2d.drawString(String.valueOf(letterPiece.getPoints()), 22, 25);
		
		return backgroundImage;
	}



	public void mouseClicked(MouseEvent e) {
		((MouseListener) getParent()).mouseClicked(e);

		// TODO Auto-generated method stub
//		((BoardView) getParent().getParent()).mouseClicked(e);
		
	}



	public void mouseEntered(MouseEvent e) {
	 	setBorder(BorderFactory.createLineBorder(Color.WHITE, 1));
		repaint();
		((MouseListener) getParent()).mouseEntered(e);
	}



	public void mouseExited(MouseEvent e) {
	 	setBorder(BorderFactory.createLineBorder(Color.RED, 0));
		repaint();
		((MouseListener) getParent()).mouseExited(e);
	}



	public void mousePressed(MouseEvent e) {
		((MouseListener) getParent()).mousePressed(e);
		/*xAdjust =  e.getX() - this.getX();
        yAdjust =  e.getY() - this.getY();
//        getRootPane().processMouseEvent(e);
        setLayer(this, JLayeredPane.DRAG_LAYER);
		System.out.println(e.getX() + " " + e.getY() + " Comp: " + this.getX() + " " + this.getY());
		*/

	}




	public void mouseReleased(MouseEvent e) {
		((MouseListener) getParent()).mouseReleased(e);
//		getParent().dispatchEvent(e);
//		setLayer(this, JLayeredPane.PALETTE_LAYER);
	}



	public void mouseDragged(MouseEvent e) {
		((MouseMotionListener) getParent()).mouseDragged(e);
//		getParent().dispatchEvent(e);
//		setLocation(e.getX() - xAdjust, e.getY() - yAdjust);
//		System.out.println(e.getX() + " " + e.getY() + " Comp: " + this.getX() + " " + this.getY());
	}



	public void mouseMoved(MouseEvent e) {
		((MouseMotionListener) getParent()).mouseMoved(e);
		// TODO Auto-generated method stub
		
	}



	public void setDraggable(boolean draggable) {
		this.draggable = draggable;
	}



	public boolean isDraggable() {
		return draggable;
	}



	/**
	 * @param letterState the letterState to set
	 */
	public void setLetterState(GLetterState letterState) {
		this.letterState = letterState;
	}



	/**
	 * @return the letterState
	 */
	public GLetterState getLetterState() {
		return letterState;
	}



	/**
	 * @param letterOrigin the letterOrigin to set
	 */
	public void setLetterOrigin(GLetterState letterOrigin) {
		this.letterOrigin = letterOrigin;
	}



	/**
	 * @return the letterOrigin
	 */
	public GLetterState getLetterOrigin() {
		return letterOrigin;
	}
	
	
	
}
